package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname PermissionServiceImpl
 * @Description TODO
 * @Date 2020/10/9 10:22
 * @Created by Administrator
 */
@Service
@Slf4j
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    /**
     * 查询出所有权限
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission>  page= (Page<Permission>) permissionDao.findAll(queryPageBean.getQueryString());
       return new PageResult(page.getTotal(),page.getResult());
    }

    /*
     * 添加权限
     */
    @Override
    public void permissionAdd(Permission permission) {

       Permission name = permissionDao.selectPermissionName(permission.getName());
       Permission Keyword =permissionDao.selectPermissionKeyword(permission.getKeyword());
       if (null!=name){
           throw new BusinessRuntimeException("不能新建重复的权限名称");
       }else if (null!=Keyword){
           throw new BusinessRuntimeException("不能新建重复的关键字");
       }

        permissionDao.permissionAdd(permission);
    }
    /**
     * 回显权限
     * @param
     * @return
     */
    @Override
    public Permission permissionUpdate(Integer id) {
        return permissionDao.permissionUpdate(id);
    }

    @Override
    public void permissionEdit(Permission permission) {
        permissionDao.permissionEdit(permission);
    }

    @Override
    public void permissionDelete(Integer id) {
       long count = permissionDao.countRolePermissionId(id);

       if (count>0){
           throw new BusinessRuntimeException("当前有数据不能删除");
       }
        permissionDao.permissionDelete(id);
    }


}
